<?php
/**
 * 菜单管理
 */

class Model_AdminRole {
	
	/**
	 * 添加菜单
	 * @param array $data
	 */
	public function add_role($data) {
		if (! is_array ( $data )) {
			return false;
		}
		
		$DB_Role = new DB_AdminRole ();
		if ($DB_Role->insert ( $data )) {
			return true;
		}
		return false;
	}
	
	/**
	 * 编辑菜单
	 * @param int $id
	 * @param array $data
	 */
	public function edit_role($roleid, $data) {
		if (! is_array ( $data ) || empty ( $roleid )) {
			return false;
		}
		
		$DB_Role = new DB_AdminRole ();
		if ($DB_Role->updateByConditions ( $data, "roleid={$roleid}" )) {
			return true;
		}
		return false;
	}
	
	/**
	 * 删除菜单
	 * @param int $id
	 */
	public function delete_role($roleid) {
		if (empty ( $roleid )) {
			return false;
		}
		$DB_Role = new DB_AdminRole ();
		if ($DB_Role->delete ( "roleid={$roleid}" )) {
			return true;
		}
		
		return false;
	}
	
	/**
	 * 获取菜单深度
	 * @param int $id
	 * @param array $array
	 * @param int $i
	 */
	public function get_level($id, $array = array(), $i = 0) {
		foreach ( $array as $n => $value ) {
			if ($value ['id'] == $id) {
				if ($value ['parentid'] == '0')
					return $i;
				$i ++;
				return $this->get_level ( $value ['parentid'], $array, $i );
			}
		}
	}
}
?>